home *** CD-ROM | disk | FTP | other *** search
- // superclock.cpp
-
-
- // includes
- #include "superclock.h"
- #include "drawing.h"
- #include "events.h"
-
-
- // constants
- const UInt32 k_update_interval = 2; // seconds
- const UInt32 k_date_interval = 5; // seconds
-
-
- //
- // constructor
- //
- superclock::superclock(FormGadgetType* gadgetP, view* in_superview):
- pane(&gadgetP->rect, in_superview)
- {
- reset_idle_timer();
- set_date_timer(0);
- }
-
- //
- // destructor
- //
- superclock::~superclock() {
- // do nothing
- }
-
- #pragma mark -
-
- //
- // reset_idle_timer()
- //
- void
- superclock::reset_idle_timer() {
- UInt32 now = TimGetSeconds();
- UInt32 next_minute_update = (now/60 + 1) * 60;
- if (now < m_date_timer) {
- m_idle_timer = m_date_timer;
- } else {
- m_idle_timer = next_minute_update;
- }
- }
-
- //
- // set_idle_timer()
- //
- void
- superclock::set_idle_timer(UInt32 interval) {
- m_idle_timer = TimGetSeconds() + interval;
- }
-
- //
- // set_date_timer()
- //
- void
- superclock::set_date_timer(UInt32 interval) {
- m_date_timer = TimGetSeconds() + interval;
- }
-
- #pragma mark -
-
- //
- // draw_self()
- //
- void
- superclock::draw_self() {
- // variables
- char s[timeStringLength] = {0};
- short x, y;
- UInt32 now = TimGetSeconds();
- DateTimeType date_time;
-
- // calculate positioning
- x = m_bounds.topLeft.x + m_bounds.extent.x;
- y = m_bounds.topLeft.y;
-
- // convert seconds to DateTime
- TimSecondsToDateTime (now, &date_time);
-
- // draw date if date timer is in future
- if (now<m_date_timer) {
- Int16 months, days, years;
- DateFormatType format_pref = (DateFormatType) PrefGetPreference(prefDateFormat);
-
- // create date string
- months = date_time.month;
- days = date_time.day;
- years = date_time.year;
- DateToAscii (months, days, years, format_pref, s);
- } else {
- Int16 hours, minutes;
- TimeFormatType format_pref = (TimeFormatType) PrefGetPreference(prefTimeFormat);
-
- // create time string
- hours = date_time.hour;
- minutes = date_time.minute;
- TimeToAscii (hours, minutes, format_pref, s);
- }
-
- // draw time string to display
- WinEraseRectangle(&m_bounds, 0);
- // for debugging use:
- // delay(250);
- draw_string(s, x, y, right_align, top_align);
- reset_idle_timer();
- }
-
- //
- // idle_self()
- //
- void
- superclock::idle_self() {
- UInt32 now = TimGetSeconds();
-
- if (now>=m_idle_timer) {
- draw_self();
- }
- }
-
- //
- // click_self()
- //
- Boolean
- superclock::click_self(int x, int y) {
- UInt32 now = TimGetSeconds();
-
- if (now<m_date_timer) {
- set_date_timer(0);
- // now displaying date, so display time instead
- } else {
- // now displaying time, so display date instead
- set_date_timer(k_date_interval);
- }
- draw_self();
- return true;
- }